﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;

namespace WP7TransferDataTool
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "TransferDataService" in both code and config file together.
    public class TransferDataService : ITransferDataService
    {
        public static string DefaultFolder 
        {
            get;
            set;
        }

        public List<TFileInfo> GetFiels(string searchPattern)
        {
            List<TFileInfo> fileInfoList = new List<TFileInfo>();
            DirectoryInfo dir = new DirectoryInfo(DefaultFolder);
            FileInfo[] files = dir.GetFiles(searchPattern);
            foreach (FileInfo file in files)
            {
                TFileInfo tfile = new TFileInfo();
                tfile.Name = file.Name;
                tfile.Size = string.Format("{0:N} KB", file.Length / 1024);
                tfile.DateCreated = file.CreationTime;

                fileInfoList.Add(tfile);
            }
            return fileInfoList;
        }
        
        public byte[] TransferFile(string fileName, int offset, int length)
        {
            string filePath = Path.Combine(DefaultFolder, fileName);
            byte[] buffer = null;
            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
                if (length == -1)
                {
                    length = (int)fs.Length;
                }
                buffer = new byte[length];
                fs.Read(buffer, offset, length);
            }
            return buffer;
        }
    }
}
